﻿using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using HandyControl.Controls;
using NPOI.Util;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Views;

namespace SoonCode.FocusTest.ViewModels
{
    internal class MemberListViewModel : BindableBase, IDialogAware
    {
        public ICommand ShowMemberEdit { get; set; }
        public ICommand CloseDialogCommand { get; set; }
        public ICommand ReloadDataCommand { get; set; }
        public ICommand DoSearchCommand { get; set; }
        public string SearchMember { get; set; }
        public string SearchName { get; set; }
        public MemberListViewModel(IEventAggregator aggregator, IDialogService dialogService)
        {
            SelectedMember = new ObservableCollection<MemberInfo>();
            SelectedMember.CollectionChanged += SelectedMember_CollectionChanged;
            CloseDialogCommand = new DelegateCommand(() => RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)));
            DoSearchCommand = new DelegateCommand(() => LoadData());
            ShowMemberEdit = new DelegateCommand(() =>
            {
                dialogService.ShowDialog(nameof(MemberEditView), null, (res) =>
                {
                    if (res.Result == ButtonResult.OK)
                    {
                        LoadData();
                    }
                }, "myDialogWindow");
            });
            ReloadDataCommand = new DelegateCommand(() => { LoadData(); });
        }

        private void SelectedMember_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (this.SelectedMember.Count != 1) return;

            IDialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("member", this.SelectedMember[0]);
            RequestClose.Invoke(new DialogResult(ButtonResult.Yes, dialogParameters));
        }
     

        private ObservableCollection<MemberInfo> _members;

        public ObservableCollection<MemberInfo> Members
        {
            get { return _members; }
            set
            {
                _members = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<MemberInfo> _selectedMember;

        public ObservableCollection<MemberInfo> SelectedMember
        {
            get { return _selectedMember; }
            set
            {
                SetProperty(ref _selectedMember, value);
            }

        }
        public string Title => "请选择被测试人员";




        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        private async void LoadData()
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where.Add("name", SearchName);
            where.Add("member_no", SearchMember);
            var list = await API.GetMemberList(where);
            list = new ObservableCollection<MemberInfo>(list.OrderByDescending(s => s.member_no));
            for (int i = 0; i < list.Count; i++)
            {
                list[i].index = i;
            }

            Members = list;
        }
        public async void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
